/**
 * Created by jaliu on 2014/11/5.
 */

/**
 * 数据的初始化
 */
var async = require('async');
var user = mongoose.model('user');
var dataDictionary = mongoose.model('masterData_Dictionaries');


/**
 * 初始化user
 */
user.count(function (err, count) {
    if (count == 0) {
        async.series([
            function (callback) {
                dataDictionary.findOne({'value': '宠物'}, function (err, dic) {
                    var temp = new user();
                    temp.nickName = '小区百晓生';
                    temp.mobilePhone = '0123';
                    temp.userPassword = '0123';
                    temp.DomainOfExpertise = "54a399d091da240026c97c21";
                    temp.save(callback);
                })
            },
            function (callback) {
                var images = [
                    {"imageURL": "/img/launcher.png"}
                ];
                user.update({'nickName': '小区百晓生'}, {$pushAll: {'images': images}}).exec(callback);
            },
            function (callback) {
                var temp = new user();
                temp.nickName = '小6客服';
                temp.mobilePhone = '66xiaoqu';
                temp.userPassword = '66xiaoqu';
                temp.huanxinUserName = '66customerService';
                temp.huanxinPassword = '66xiaoqu';
                temp.save(callback);
            },
            function (callback) {
                user.findOne({'nickName': '小6客服'}, function (err, result) {
                    var args = {
                        data: {
                            username: result.huanxinUserName,
                            password: '66xiaoqu'
                        },
                        headers: {"Content-Type": "application/json"}
                    };

                    restClient.post("http://a1.easemob.com/66xiaoqu/"+huanxinEnv+"/users", args, function (data, response) {
                        logger.info(data);
                    }).on('error', function (err) {
                        logger.error('init community around went wrong on the request' + err.message);
                    });
                })
            }
        ])
    }
});

